package com.example.myweather.modelo;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import com.example.myweather.utilities.WeatherXDotNetParseInfo;

import android.R.xml;
import android.util.Log;
import android.util.Xml;

public class WebServicesXDotNet implements WeatherWebService {
	
	//Method contains the method that we want to execute at web service
   	private String METHOD = "GetWeather";
   	
   	//Domain Name is the domain name that host web service
   	private String DOMAINNAME = "http://www.webserviceX.NET";
   	
   	// Domain Name + Method
   	private String ACTIONSOAP = "http://www.webserviceX.NET/GetWeather";
   	
   	// File that defines web service
   	private String URL = "http://www.webservicex.net/globalweather.asmx";
	
   	//XML Root Node
   	
   	private String XMLROOT = "CurrentWeather"; 
   	
	@Override
	public Weather getWeather(String country, String city) {
		// TODO Auto-generated method stub
		try {
       	 
            // Request's Model
            SoapObject request = new SoapObject(DOMAINNAME,METHOD);
            
            //Pass parameters at Web Service
            request.addProperty("CityName", city); 
            request.addProperty("CountryName", country);
            
            //Envelope's Model
            SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            envelope.dotNet = true;
            envelope.setOutputSoapObject(request);
         
            //Transport's Model
            HttpTransportSE transport = new HttpTransportSE(URL);
         
            //Call
            transport.call(ACTIONSOAP, envelope);
         
            //Web Service Result Result
            SoapPrimitive result = (SoapPrimitive) envelope.getResponse();
            
            XMLParser xmlParser = new XMLParser();
           
            String xml = result.toString();
            
            Weather weather = new Weather();
            
            WeatherXDotNetParseInfo weatherXDotNetParseInfo = new WeatherXDotNetParseInfo();
            
            weather.setTemperature(weatherXDotNetParseInfo.getCorrectTemperatureFormat(xmlParser.getNodeValue(xml, XMLROOT, "Temperature")));
            
            weather.setSkyConditions(xmlParser.getNodeValue(xml, XMLROOT, "SkyConditions"));
            
            weather.setHumidity(xmlParser.getNodeValue(xml, XMLROOT, "RelativeHumidity"));
            
            weather.setWind(xmlParser.getNodeValue(xml, XMLROOT, "Wind"));
            
            weather.setPressure(weatherXDotNetParseInfo.getCorrectPressureFormat(xmlParser.getNodeValue(xml, XMLROOT, "Pressure")));
            
            return weather;
            		
        } catch (Exception e) {	
           
        	//Error Lists that catch this Exception
           
           // ->If you dont have Network to Acces at Webservices
           // ->If you have some XML parse Error
        	
           Log.e("Error WebServicesXDotNet",e.getMessage());
           
           //Return Unknow Data
           return null;
        }
		
	}

}
